//
//  ScrollImageCell.m
//  Fhc
//
//  Created by steve aa on 14-8-16.
//  Copyright (c) 2014年 Alex. All rights reserved.
//

#import "ScrollImageCell.h"
#import "XXTool.h"
#import "ExhibitionProfileModel.h"

@implementation ScrollImageCell
{
    NSMutableArray *useImageArray;
    CGFloat width;
    CGFloat height;
}
@synthesize scrollView;
@synthesize pageControl;

-(id)initWithStyle:(UITableViewCellStyle)style
   reuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        width = [[UIScreen mainScreen] bounds].size.width;
        height = width*420/750;
        self.frame=CGRectMake(0, 0, width, height);
        
        scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
        scrollView.pagingEnabled=YES;//立刻翻页到下一页 没中间的拖动过程
        scrollView.bounces=NO;//去掉翻页中的白屏
        scrollView.showsHorizontalScrollIndicator=NO;//不现实水平滚动条
        scrollView.userInteractionEnabled = YES;
        [scrollView setDelegate:self];
        [self addSubview:scrollView];
        
        //将pagecontrol添加到scrollview中
        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, height-15, width, 15)];
        pageControl.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        pageControl.numberOfPages=0;
        pageControl.currentPage=0;
        [pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
        [pageControl setPageIndicatorTintColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.5]];
        [self addSubview:pageControl];
        
        pageNumber=0;
        
        useImageArray = [[NSMutableArray alloc] init];

    }
    return self;
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView1{
    [timer invalidate];
   timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
    
    CGPoint offset=scrollView1.contentOffset;
    CGRect bounds=scrollView1.frame;
    int nowPage=offset.x/bounds.size.width-1;
    
    if(nowPage==-1)
    {
        [scrollView scrollRectToVisible:CGRectMake(pageNumber*width, 0, width, height) animated:NO];
        [pageControl setCurrentPage:pageNumber-1];
    }
    else if(nowPage==pageNumber)
    {
        [scrollView scrollRectToVisible:CGRectMake(width, 0, width, height) animated:NO];
        [pageControl setCurrentPage:0];
    }
    else
    {
        [pageControl setCurrentPage:nowPage];
    }
    
}


-(void)scrollTimer
{
    CGPoint offset=scrollView.contentOffset;
    CGRect bounds=scrollView.frame;
    int nowPage=offset.x/bounds.size.width-1;
    nowPage++;
    
    if(nowPage==pageNumber)
    {
        [scrollView scrollRectToVisible:CGRectMake(0, 0, width, height) animated:NO];
        [scrollView scrollRectToVisible:CGRectMake(width, 0, width, height) animated:YES];
        [pageControl setCurrentPage:0];
    }
    else
    {
        [scrollView scrollRectToVisible:CGRectMake(width*(nowPage+1), 0, width, height) animated:YES];
        [pageControl setCurrentPage:nowPage];
    }
}


-(void)setImageArray:(NSArray*)imageArray whichImage:(NSArray*)urlArray
{
    
    scrollView.frame = CGRectMake(0, 0, width, height);
    pageControl.frame = CGRectMake(0, height-[UIScreen mainScreen].bounds.size.width/20, width, [UIScreen mainScreen].bounds.size.width/20);
    if(useImageArray.count!=0)
    {
        for (int i=0; i<useImageArray.count; i++)
        {
            UIImageView *imageview = useImageArray[i];
            [imageview removeFromSuperview];
        }
        [useImageArray removeAllObjects];
    }
    pageNumber=(int)imageArray.count;
    
    whichUrlArray = [NSMutableArray array];
    
    //重新设置scrollview的长度
    [scrollView setContentSize:CGSizeMake(width*(pageNumber+2), height)];//设置scrollview画布的大小用来实现三页照片的转换。
    
    for(int i=0;i<pageNumber;i++)
    {
        ShowTopImgModel *temp = [imageArray objectAtIndex:i];
        
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake((i+1)*width, 0, width, height)];
        [imageview setTag:i];
        [imageview setUserInteractionEnabled:YES];
        [imageview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)]];
        [imageview setImageWithUrl:temp.photo_url placerHolder:nil];
        [whichUrlArray addObject:temp.photo_address];
        [imageview setContentMode:UIViewContentModeScaleAspectFill];
        [imageview setClipsToBounds:YES];
        [scrollView addSubview: imageview];
        [useImageArray addObject:imageview];
        if(i==0)//首尾各添加一张图片用于循环显示
        {
            ShowTopImgModel *tempZero = [imageArray objectAtIndex:pageNumber-1];
            
            UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
            [imageview setTag:i-1];
            [imageview setUserInteractionEnabled:YES];
             //[imageview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)]];
            [imageview setContentMode:UIViewContentModeScaleAspectFill];
            [imageview setClipsToBounds:YES];
            [scrollView addSubview: imageview];
            [imageview setImageWithUrl:tempZero.photo_url placerHolder:nil];
            [useImageArray addObject:imageview];

        }
        else if(i==pageNumber-1)
        {
            ShowTopImgModel *tempLast = [imageArray objectAtIndex:0];
            
            UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake((i+2)*width, 0, width, height)];
            [imageview setTag:i+1];
            [imageview setUserInteractionEnabled:YES];
            //[imageview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)]];
            [imageview setContentMode:UIViewContentModeScaleAspectFill];
            [imageview setClipsToBounds:YES];
            [scrollView addSubview: imageview];
            [imageview setImageWithUrl:tempLast.photo_url placerHolder:nil];
            [useImageArray addObject:imageview];

        }
    }
    [scrollView scrollRectToVisible:CGRectMake(width, 0, width, height) animated:NO];//默认显示第一张
    
    pageControl.numberOfPages=imageArray.count;
    pageControl.currentPage=0;
    
    //自动滑动
    [timer invalidate];
    timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
}

//点击scrollview图片
-(void)clickImage:(UIGestureRecognizer *)recognizer
{
    NSInteger tag = [recognizer view].tag;
    
    [WebController pushWebControllerWithSourceVC:[XXTool getViewController:self] requestString:whichUrlArray[tag] mapTitle:nil];
    
}

@end
